package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseLaw;
import top.jnaw.jee.utils.Dates;
import top.jnaw.jee.utils.Html2Test;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.Strings;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Law extends BaseLaw<Law> {

  public static final Law dao = new Law().dao();

  public static ModelResult add(String title, String content,
      int pid, JSONArray accessory) {
    ModelResult result = new ModelResult(Result.kModelSaveError);
    Law law = new Law();
    law.setTitle(title);
    law.setContent(content);
    law.setPureText(Html2Test.getTextFromHtml(content));
    law.setCreateTime(Dates.now());
    if (pid >= 0) {
      law.setPid(pid);
    }
    if (law.save()) {
      boolean r = FileModel.add(2, law.getId(), accessory, "accessory");
      if (r) {
        result.setResult(Result.kSuccess);
      }
    }
    return result;
  }

  public static boolean delete(int id) {
    return dao.deleteById(id);
  }

  public static ModelResult update(int id, String title, String content,
      int pid, JSONArray accessory) {
    ModelResult result = new ModelResult(Result.kModelUpdateError);
    Law law = dao.findById(id);
    if (null != law) {
      law.setTitle(title);
      law.setContent(content);
      law.setPureText(Html2Test.getTextFromHtml(content));
      if (pid >= 0) {
        law.setPid(pid);
      }
      if (law.update()) {
        boolean r = FileModel.update(2, law.getId(), accessory, "accessory");
        if (r) {
          result.setResult(Result.kSuccess);
        }
      }
    }
    return result;
  }

  public static ModelResult query(int id) {
    ModelResult result = new ModelResult(Result.kNull);
    JSONObject object = null;
    String sql = Db.getSql("law-query");
    List<Law> list = dao.find(sql, id);
    if (null != list && list.size() > 0) {
      object = Models.compressList(list);
      object.put("accessory", FileModel.list(2, id, "accessory", false));
    }
    if (null != object) {
      result.setObject(object);
      result.setResult(Result.kSuccess);
    }
    return result;
  }

  public static JSONObject list(int page, int size, JSONArray search) {
    String keyWord;
    String logical;
    String sec = Db.getSql("law-list-sec");
    String exp = Db.getSql("law-list-exp");
    Object s = Strings.get(search, "keyWord", true);
    if (null != s) {
      JSONObject object = JSONObject.parseObject(s.toString());
      keyWord = object.getString("keyWord");
      logical = object.getString("logical");
      exp +=
          "\n" + logical + " (title like concat('%','" + keyWord
              + "','%') or  content like concat('%','" + keyWord + "','%'))";
    }
    exp += " order by create_time desc ";
    Page<Record> pages = Db.paginate(page, size, sec, exp);
    return Models.compressPage(pages);
  }
}
